package com.itheima.rabbitmq.topic;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author : [chenxys]
 * @version : [v1.0]
 * @Description : 通配符模式 消费者1 接收所有类型的消息
 * @createTime : [2022/6/23 18:48]
 */
public class Consumer2 {
    //交换机名称
    static final String TOPIC_EXCHAGE = "topic_exchange";
    //队列名称
    static final String TOPIC_QUEUE_2 = "topic_queue_2";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setVirtualHost("/heima");
        factory.setUsername("heima");
        factory.setPassword("heima");

        Connection connection = factory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(TOPIC_EXCHAGE, BuiltinExchangeType.TOPIC);

        channel.queueDeclare(TOPIC_QUEUE_2,true,false,false,null);

        //新增商品，更新商品和删除商品。
        channel.queueBind(TOPIC_QUEUE_2,TOPIC_EXCHAGE,"*.*");

        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2-接收所有消息：" + new String(body, "utf-8"));
            }
        };

        channel.basicConsume(TOPIC_QUEUE_2,true,consumer);
    }
}
